package cn.lxh.org.service.impl;

import cn.lxh.basic.exception.BusinessException;
import cn.lxh.basic.service.impl.BaseServieceImpl;
import cn.lxh.basic.utils.BaiduAuditUtils;
import cn.lxh.org.basic.utils.AjaxResult;
import cn.lxh.org.domain.Employee;
import cn.lxh.org.domain.Shop;
import cn.lxh.org.domain.ShopAuditLog;
import cn.lxh.org.mapper.EmployeeMapper;
import cn.lxh.org.mapper.ShopAuditLogMapper;
import cn.lxh.org.mapper.ShopMapper;
import cn.lxh.org.service.IShopService;
import cn.lxh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxh
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServieceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    //这里注入的是员工表实现层
    @Autowired
    private EmployeeServiceImpl employeeService;

    /**
     * 店铺入驻逻辑
     * 一：参数空校验
     * 。。不能为空
     * 二：合法校验   @TODO 百度AI智能审核接口做
     * 三：判断是否曾经已经注册过
     * <p>
     * 1. 店铺不能被注册
     * 2.人是否被注册
     * 2.1 注册了但是没有绑定店铺
     * 2.2 注册了并且已经绑定了店铺
     * <p>
     * 四：保存数据
     * shop
     * employee  shopId
     * <p>
     * 反过来修改shop中的admin_id
     *
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
//        *  一：参数空校验  即使前端前端写了校验，我们也需要写自己的后台校验逻辑
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin.getUsername())
        ) {

            throw new BusinessException("参数不能为空！！！");
        }
//        *    。。不能为空
//        *  二：合法校验   @TODO 百度AI智能审核接口做
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("名称不合法啦");
        }

       /* if(!BaiduAuditUtils.ImgCensor(""+shop.getLogo())){
            throw new BusinessException("照片不可以色色哟");
        }*/
//        *  三：判断是否曾经已经注册过
        Shop shopTmp = shopMapper.findByName(shop.getName());

//        *    1. 店铺不能被注册
        if (shopTmp != null) {
            throw new BusinessException("店铺已经被注册，请直接登录，如果忘记密码，请走找回密码流程！！！");
        }
//        *    2.人是否被注册
        Employee empTmp = employeeMapper.findByUsername(admin.getUsername());

        if (empTmp != null && empTmp.getShop_id() != null) {
//        *      2.2 注册了并且已经绑定了店铺
            throw new BusinessException("该用户已经存在运营店铺，请成功新换人！！");
        }
//        *      2.1 注册了但是没有绑定店铺  === 直接绑定
        if (empTmp == null) {
        //这里需要经过员工service层才能执行
            employeeService.save(admin);//要返回自增ID
            empTmp = admin;
        }
        //  empTmp 一定有值
//        *  四：保存数据
//        *    shop
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);

    }

    /**
     * 审核通过事件
     *
     * @param shopAuditLog
     */
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    //发送邮件需要 注入才能用
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        shopAuditLog.setState(1);
        shopAuditLogMapper.save(shopAuditLog);
        Long shopId = shopAuditLog.getShopId();
        //先拿到这个店 的 id
        Shop shop = shopMapper.findById(shopId);

        // 找到这个店铺
        shop.setState(1);
        //修改这个店铺的状态
        shopMapper.update(shop);
        // 发送邮件
        Employee employee = employeeMapper.findByShopId(shopAuditLog.getShopId());
        System.out.println(employee);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1160525093@qq.com");
        message.setTo("1160525093@qq.com");
        message.setSubject("宠物审核通过通知");
        message.setText("你的店铺通过审核，快来激活吧：" +
                "激活链接：http://localhost:8080/shop/shopRegister/"+shopAuditLog.getShopId());
        javaMailSender.send(message);


    }

    /**
     * 驳回操作
     *
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //修改日志的状态
        shopAuditLog.setState(-1);
        shopAuditLogMapper.save(shopAuditLog);
        //修改店铺的状态
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        shop.setState(-1);
        //修改店铺的状态
        shopMapper.update(shop);
        //找到管理员，发送邮件
        Employee employee = employeeMapper.findByShopId(shopAuditLog.getShopId());


        //需要复制文件
        try {
            //创建复杂邮件对象
            MimeMessage message = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom("1160525093@qq.com");
            helper.setSubject("审核驳回通知");
            //第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>审核失败</h1><br>" +
                    "<span>失败原因：" + shopAuditLog.getNote() + "</span><br>" +
                    "<a href='http://127.0.0.1:8081/#/register2?id=" + shop.getId() + "'>点击修改资料</a>", true);
            //添加附件
            //收件人
            helper.setTo("1160525093@qq.com");
            //发送邮件
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }

    /**
     * 批量增加
     * @param shops
     */
    @Transactional
    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }

    @Override
    public List<ShopVo> queryEcharts() {
       List<ShopVo> shopVos= shopMapper.findEcharts();

        return shopVos;
    }
}